﻿using Himp.TaskScheduling.Hangfire.Models;

namespace Himp.TaskScheduling.Hangfire.Jobs
{
    public class BaseBoltInfoJob
    {
        #region SQL

        /// <summary>
        /// 获取抽样任务
        /// </summary>
        public const string GET_BOLTINFOVIEW_SQL = @"
            SELECT *
            FROM [dbo].[VIEW_BOLT_INFO]
            ";

        public const string INSERT_SAMPLECABINET_CONFIGINFO = @"
            INSERT INTO
            [dbo].[SampleCabinet_ConfigInfo]
            (
            SampleCabNum,
            SampleCabCategory,
            SampleTypeDefault,
            SamplePositionNum,
            ValiFlag,
            Samplematrix,
            SampleTypeCurrent,
            XPosition,
            YPosition,
            ZPosition,
            CoalSampleCode,
            Occupancy,
            SampleCustodian,
            SamplingTime,
            SamplingLedgerNum,
            QualityIncoming,
            Granularity,
            Supervisor
            )
            VALUES
            (
            @SampleCabNum,
            @SampleCabCategory,
            @SampleTypeDefault,
            @SamplePositionNum,
            @ValiFlag,
            @Samplematrix,
            @SampleTypeCurrent,
            @XPosition,
            @YPosition,
            @ZPosition,
            @CoalSampleCode,
            @Occupancy,
            @SampleCustodian,
            @SamplingTime,
            @SamplingLedgerNum,
            @QualityIncoming,
            @Granularity,
            @Supervisor
            )
            ";

        public const string UPDATE_SAMPLECABINET_CONFIGINFO = @"
            UPDATE 
            [dbo].[SampleCabinet_ConfigInfo]
            SET
            SampleCabNum=@SampleCabNum,
            SampleCabCategory=@SampleCabCategory,
            SampleTypeDefault=@SampleTypeDefault,
            SamplePositionNum=@SamplePositionNum,
            ValiFlag=@ValiFlag,
            Samplematrix=@Samplematrix,
            SampleTypeCurrent=@SampleTypeCurrent,
            XPosition=@XPosition,
            YPosition=@YPosition,
            ZPosition=@ZPosition,
            CoalSampleCode=@CoalSampleCode,
            Occupancy=@Occupancy,
            SampleCustodian=@SampleCustodian,
            SamplingTime=@SamplingTime,
            SamplingLedgerNum=@SamplingLedgerNum,
            QualityIncoming=@QualityIncoming,
            Granularity=@Granularity,
            Supervisor=@Supervisor
            WHERE
            SamplePositionNum=@SamplePositionNum
            AND SampleCabNum=@SampleCabNum
";

        public const string EXIST_FLAG_SQL = @"
            SELECT CASE WHEN 
            EXISTS (SELECT 1 FROM [dbo].[SampleCabinet_ConfigInfo] WHERE SamplePositionNum = @SamplePositionNum AND SampleCabNum=@SampleCabNum) 
            THEN 1 
            ELSE 0 
            END";

        public const string GET_SAMPLE_PREPARATION_REC_SQL = @"
            SELECT [ID]
                  ,[MachineCode]
                  ,[SampleID]
                  ,[PackCode]
                  ,[ZYWeight]
                  ,[ZYType]
                  ,[SampleType]
                  ,[Size]
                  ,[Water]
                  ,[StartTime]
                  ,[EndTime]
                  ,[SampleWeight]
                  ,[UserName]
            FROM [dbo].[HY_SamplePreparationRec]
            WHERE CONVERT(VARCHAR(10), [EndTime], 120) = CONVERT(VARCHAR(10), GETDATE(), 120)
            OR CONVERT(VARCHAR(10), [EndTime], 120) = CONVERT(VARCHAR(10), GETDATE()-1, 120)
            OR CONVERT(VARCHAR(10), [EndTime], 120) = CONVERT(VARCHAR(10), GETDATE()-2, 120)
            ";

        #endregion

        #region 方法

        public SampleCabinetConfigInfo BoltInfoConvert(int machineCode ,BoltInfoView item)
        {
            int sampleTypeCurrent = 0;
            string granularity = ""; //BIG 

            if (item.BIG == true)
            {
                granularity = "6mm";
                sampleTypeCurrent = 1;
            }
            else if (item.MIDDLE == true)
            {
                granularity = "3mm";
                sampleTypeCurrent = 2;
            }
            else if (item.SMALL == true)
            {
                granularity = "0.2mm";
                sampleTypeCurrent = 3;
            }

            int x = 0;
            int y = item.ColumnNo;

            if (item.ColumnNo % 2 == 0) //偶数
            {
                x = 49 * 2 - 2 * (item.RowNo - 1);
            }
            else //奇数
            {
                x = 49 * 2 - 2 * (item.RowNo - 1) - 1;
            }

            long samplematrix = 1;

            if (item.RotateNo == 1)
            {
                samplematrix = 2;
            }

            SampleCabinetConfigInfo sampleCabinetConfigInfoRec = new SampleCabinetConfigInfo();
            sampleCabinetConfigInfoRec.SampleCabNum = machineCode; //?设备编号
            sampleCabinetConfigInfoRec.SampleCabCategory = 0; //存样柜类别
            sampleCabinetConfigInfoRec.SampleTypeDefault = 0; //默认存放类型
            sampleCabinetConfigInfoRec.SamplePositionNum = item.Bolt_ID; //存样瓶位置编号
            sampleCabinetConfigInfoRec.ValiFlag = true;
            sampleCabinetConfigInfoRec.Samplematrix = samplematrix; //所属面位
            sampleCabinetConfigInfoRec.SampleTypeCurrent = sampleTypeCurrent; //当前存放类型
            sampleCabinetConfigInfoRec.XPosition = x; //行
            sampleCabinetConfigInfoRec.YPosition = y; //列
            sampleCabinetConfigInfoRec.ZPosition = item.PositionNo; //
            sampleCabinetConfigInfoRec.CoalSampleCode = item.Sample_ID; //煤样编码
            sampleCabinetConfigInfoRec.Occupancy = item.Bolt_State;
            sampleCabinetConfigInfoRec.SampleCustodian = "-";
            sampleCabinetConfigInfoRec.SamplingTime = item.StartTime;
            sampleCabinetConfigInfoRec.Granularity = granularity; //颗粒
            sampleCabinetConfigInfoRec.AutoMode = true;

            return sampleCabinetConfigInfoRec;
        }

        #endregion
    }
}
